﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class Loading : Form
    {
        string _sqlite_file;
        FileMode _sqlite_mode;
        string _rotas_file;        
        FileMode _rotas_mode;
        Exception _exception;
        public Exception GetException
        {
            get
            {
                return _exception;
            }
        }
        string _fail_message;
        public string GetFailMessage
        {
            get
            {
                return _fail_message;
            }
        }

        public Loading()
        {
            InitializeComponent();
        }

        public Loading(
            string pSQLiteDatabaseFilename,
            FileMode pSQLiteDatabaseFileMode,
            string pRotasTemplatesDatabaseFilename,
            FileMode pRotasTemplatesDatabaseFileMode)
            : this()
        {
            _sqlite_file = pSQLiteDatabaseFilename;
            _sqlite_mode = pSQLiteDatabaseFileMode;
            _rotas_file = pRotasTemplatesDatabaseFilename;
            _rotas_mode = pRotasTemplatesDatabaseFileMode;

            Text = "Loading " + Path.GetFileName(_sqlite_file) + "... ";
        }

        void Fail(
            string pMessage,
            Exception pException)
        {
            Cursor = Cursors.Arrow;
            _fail_message = pMessage;
            _exception = pException;
            DialogResult = DialogResult.Abort;
            Close();
        }

        private void Loading_Shown(object sender, EventArgs e)
        {            
            Cursor = Cursors.WaitCursor;            
            this.Refresh();

            if(_sqlite_mode == FileMode.Create)
            {
                try
                {
                    SQLiteAccess.CreateNewDatabase(_sqlite_file);
                }
                catch (Exception ex)
                {
                    Fail("Creating new SQL database failed",
                        ex);
                    Close();
                    return;
                }
            }
                        

            try
            {                
                SQLiteAccess.LoadData(_sqlite_file);
            }
            catch (Exception ex)
            {
                Fail(
                    "Loading SQL database failed",
                    ex);
                Close();
                return;
            }

            Cursor = Cursors.WaitCursor;
            Text = Resources.FORM_LOADING_LoadingFile.Replace(
                "#",
                System.IO.Path.GetFileName(_rotas_file));
            if (_rotas_mode == FileMode.Create)
            {
                try
                {
                    var _foo = Program.CreateDatabase(
                        _rotas_file,
                        (DateTime?)null,
                        String.Empty,
                        null);
                    if (!_foo)
                    {
                        Fail("You didn't want to choose any date",
                            null);
                        Close();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Fail("Loading new rotas file failed",
                        ex);
                    Close();
                    return;
                }
            }
            try
            {
                Program.ReadDatabase(_rotas_file);
            }
            catch (SerializationException ex)
            {
                Fail(
                    Resources.MESSAGEBOX_ERROR_InvalidFileFormat_MESSAGE,
                    ex);
                Close();
                return;
            }
            catch (Exception ex)
            {
                Fail("Loading new rotas file failed",
                    ex);
                Close();
                return;
            }

            try
            {
                Text = Resources.FORM_LOADING_CheckingIntegrity;
                // without fixing
                var _is_corrupted = Program.CurrentFile.TestForIntegrity(false);
                if (_is_corrupted)
                {
                    const string message = "Integrity broken. Do you want to fix the database? If not, file loading will be aborted.";
                    var _fix = MessageBox.Show(message,
                       "Fix?",
                       MessageBoxButtons.YesNo,
                       MessageBoxIcon.Exclamation);
                    if (_fix == DialogResult.No)
                    {
                        Fail("Action cancelled",
                            null);
                        Close();
                        return;
                    }
                    else
                    {
                        Text = Resources.FORM_LOADING_FixingIntegrity;
                        var _foo = Program.CurrentFile.TestForIntegrity(true);
                    }
                }
            }
            catch (Exception ex)
            {
                Fail("Integrity check failed",
                    ex);
                Close();
                return;
            }

            Program.MRURotasFiles.AddEntry(_rotas_file, false, true);
            Program.MRUSQLFiles.AddEntry(_sqlite_file, false, true);            

            DialogResult = DialogResult.OK;
            Close();
        }
        

    }
}
